package com.lgd.factoryattendance.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lgd.factoryattendance.Bean.ProPPBom;
import com.lgd.factoryattendance.R;

import java.util.List;

/**
 * @Author lgd
 * @Date 2025/11/13 10:56
 */
public class BomListAdapter extends BaseAdapter {
    private Context context;
    private List<ProPPBom> bomList;

    public BomListAdapter(Context context, List<ProPPBom> bomList) {
        this.context = context;
        this.bomList = bomList;
    }

    @Override
    public int getCount() {
        return bomList == null ? 0 : bomList.size();
    }

    @Override
    public Object getItem(int position) {
        return bomList == null ? null : bomList.get(position) ;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.bom_list_listview, null);
            viewHolder = new ViewHolder();
            viewHolder.bom_list_layout = (LinearLayout) view.findViewById(R.id.bom_list_layout);
            viewHolder.tv_FMaterial_Number = (TextView) view.findViewById(R.id.tv_FMaterial_Number);
            viewHolder.tv_FMaterial_Name = (TextView) view.findViewById(R.id.tv_FMaterial_Name);
            viewHolder.tv_Drawing_No = (TextView) view.findViewById(R.id.tv_Drawing_No);
            viewHolder.tv_FMust_Qty = (TextView) view.findViewById(R.id.tv_FMust_Qty);
            viewHolder.tv_FPicked_Qty = (TextView) view.findViewById(R.id.tv_FPicked_Qty);
            viewHolder.tv_FNoPicked_Qty = (TextView) view.findViewById(R.id.tv_FNoPicked_Qty);
            viewHolder.tv_lack_Qty = (TextView) view.findViewById(R.id.tv_lack_Qty);
            viewHolder.tv_FInventory_Qty = (TextView) view.findViewById(R.id.tv_FInventory_Qty);
            viewHolder.tv_otherReq_Qty = (TextView) view.findViewById(R.id.tv_otherReq_Qty);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        ProPPBom proPPBom = bomList.get(position);
        viewHolder.tv_FMaterial_Number.setText(proPPBom.getFmaterialNumber());
        viewHolder.tv_FMaterial_Name.setText(proPPBom.getFmaterialName());
        viewHolder.tv_Drawing_No.setText(proPPBom.getDrawingNo());
        viewHolder.tv_FMust_Qty.setText(proPPBom.getFmustQty());
        viewHolder.tv_FPicked_Qty.setText(proPPBom.getFpickedQty());
        viewHolder.tv_FNoPicked_Qty.setText(proPPBom.getFnoPickedQty());
        viewHolder.tv_lack_Qty.setText(proPPBom.getLackQty());
        viewHolder.tv_FInventory_Qty.setText(proPPBom.getFinventoryQty());
        viewHolder.tv_otherReq_Qty.setText(proPPBom.getOtherReqQty());
        // 设置背景颜色
        if (position % 2 == 0){
            viewHolder.bom_list_layout.setBackgroundResource(R.color.white);
        }else {
            viewHolder.bom_list_layout.setBackgroundResource(R.color.gray_e);
        }

        // 设置按下状态的颜色
        viewHolder.bom_list_layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("............ACTION_DOWN............");
                        v.setBackgroundResource(R.color.lightgreen);
                        break;
                    case MotionEvent.ACTION_UP:
                        System.out.println("================ACTION_UP================");
                    case MotionEvent.ACTION_CANCEL:
                        System.out.println("---------ACTION_CANCEL----------");
                        // 恢复原来的背景色
                        if (position % 2 == 0) {
                            v.setBackgroundResource(R.color.white);
                        } else {
                            v.setBackgroundResource(R.color.gray_e);
                        }
                        break;
                }
                return true;
            }
        });

        return view;
    }

    private static class ViewHolder{
        LinearLayout bom_list_layout;
        TextView tv_FMaterial_Number, tv_FMaterial_Name, tv_Drawing_No,
                tv_FMust_Qty, tv_FPicked_Qty, tv_FNoPicked_Qty, tv_lack_Qty,
                tv_FInventory_Qty, tv_otherReq_Qty;
    }
}
